package net.esj.basic.plugins.view.easyui.utils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.esj.basic.dao.querybuilder.QueryBuilder;
import net.esj.basic.utils.DateFormat;
import net.esj.basic.utils.JavaProtoType;
import net.esj.basic.utils.view.JSONUtil;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.hibernate.collection.internal.PersistentSet;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.json.JSONException;

public class EasyUiFormUtil {
	
	private static EasyUiFormUtil instance;

	public static String toEasyUiFormString(Object o,String entity) throws JSONException{
		if(instance==null){
			instance = new EasyUiFormUtil();
		}
		return instance.toMonolayerJOSNString(o, entity);
	}
	
	public String toMonolayerJOSNString(Object o,String entity) throws JSONException{
		Map<String,Object> map = new HashMap<String, Object>();
		toJSONString(o, entity,map);
		return JSONUtil.toJSONString(map);
	}
	
	/**
	 * 单层json输出
	 * @param o
	 * @param prefex
	 * @return
	 */
	public void toJSONString(Object o ,String prefex,Map<String,Object> reval){
		PropertyDescriptor[] origDescriptors =
			BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(o);
		 for (int i = 0; i < origDescriptors.length; i++) {
	            String name = origDescriptors[i].getName();
	            if("class".equals(name)){
	            	continue;
	            }
	            if (BeanUtilsBean.getInstance().getPropertyUtils().isReadable(o, name)) {
	                try {
	                    Object value =
	                    	BeanUtilsBean.getInstance().getPropertyUtils().getSimpleProperty(o, name);
	                    
	                    if(value==null){
	                    	continue;
	                    }
	                    if(value instanceof HibernateProxy) {
	                        LazyInitializer lazyInitializer = ((HibernateProxy)value).getHibernateLazyInitializer();
	                        if(lazyInitializer.isUninitialized()) {
	                        	//reval.put("'"+prefex+"."+name+".id'", lazyInitializer.getIdentifier());
	                        	/**
	                        	 * 1.3.2的貌似不再需要带单引号，同下
	                        	 */
	                        	reval.put(prefex+"."+name+".id", lazyInitializer.getIdentifier());
	                        	continue;
	                        }
	                    }
	                    if(value instanceof PersistentSet) {
	                    	continue;
	                    }
	                    if(value instanceof QueryBuilder){
	                    	continue;
	                    }
	                    
	                    if(JavaProtoType.JAVA_PROTO_TYPE.contains(value.getClass())){
	                    	//reval.put("'"+prefex+"."+name+"'", value);
	                    	reval.put(prefex+"."+name, value);
	                    }else if(JavaProtoType.JAVA_ARRAY_TYPE.contains(value.getClass())){
	                    	//for(int j=0;j<Array.getLength(value);j++){
	                    		//toJSONString(Array.get(value, j), "'"+prefex+"."+name+"["+i+"]'", reval);
	                    		//toJSONString(Array.get(value, j), prefex+"."+name+"["+i+"]", reval);
	                    	//}
	                    	reval.put(prefex+"."+name, value);
	                    	//toJSONString(value, prefex+"."+name, reval);
	                    }else if(value instanceof Enum){
	                    	//reval.put("'"+prefex+"."+name+"'", ((Enum)value).name());
	                    	reval.put(prefex+"."+name, ((Enum)value).name());
	                    }else if(value instanceof Map){
	                    	for(Object key: ((Map)value).keySet()){
	                    		//toJSONString(((Map)value).get(key),"'"+prefex+"."+name+"."+key.toString()+"'", reval);
	                    		toJSONString(((Map)value).get(key),prefex+"."+name+"."+key.toString(), reval);
	                    	}
	                    }else if(value instanceof Collection){
	                    	Iterator it = ((Collection)value).iterator();
	                    	int j=0;
	                    	while(it.hasNext()){
	                    		Object v = it.next();
	                    		//toJSONString(v,"'"+prefex+"."+name+"["+i+"]'", reval);
	                    		toJSONString(v,prefex+"."+name+"["+i+"]", reval);
	                    		i++;
	                    	}
	                    }else if(value instanceof Date){
	                    	//reval.put("'"+prefex+"."+name+"'", DateFormat.toString(((Date)value), DateFormat.DEFAULT_DATETIME_FORMAT_SEC));
	                    	reval.put(prefex+"."+name, DateFormat.toString(((Date)value), DateFormat.DEFAULT_DATETIME_FORMAT_SEC));
	                    }
	                    else{
	                    	toJSONString(value,prefex+"."+name,reval);
	                    }
	                } catch (NoSuchMethodException e) {
	                	e.printStackTrace();
	                } catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
	            }
		 }
	}
}
